//******************************************************************************************************
// Copyright 2008 Amazon Technologies, Inc.  
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 
// compliance with the License. 
//
// You may obtain a copy of the License at:http://aws.amazon.com/apache2.0  This file is distributed on 
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
//
// See the License for the specific language governing permissions and limitations under the License. 
//******************************************************************************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SQS_Sample_Object;

namespace SQS_Sample_Requestor
{
    public partial class StockQuoteClient : Form
    {
    #region Windows Application
           
        string[] _defaultStocks = new string[] {"VRTA","OREX", "RTLX", "LEDR", "FSTF", "DXYN", "BOCH", "ENZN", "SPIR", "SNMX", "ANCX", "COGT", "CENTA", "ZILG", "HSNI", "GILT", "GPRE", "SAFM", "BFNB", "HCKT", "QQQQ", "MSFT", "INTC", "CSCO", "ORCL", "YHOO", "AAPL", "RIMM", "AMAT", "CMCSA", "DELL", "SYMC", "EBAY", "QCOM", "SCHW", "DRYS", "SBUX", "JAVA", "BRCM", "DTV"};

        public StockQuoteClient()
        {
            InitializeComponent();
            
            //
            // Load any saved stocks
            //
            if (Properties.Settings.Default.Stocks != null)
            {
                ArrayList stocks = new ArrayList(Properties.Settings.Default.Stocks);

                //foreach (string s in stocks)
                //{
                //    ListViewItem li = StockDetails.Items.Add(s);
                //    li.SubItems.Add("na");
                //    li.SubItems.Add("na");
                //    li.SubItems.Add("na");
                //    li.SubItems.Add("na");
                //    li.SubItems.Add("na");
                //    li.SubItems.Add("na");
                //    li.SubItems.Add("na");
                //    li.SubItems.Add("na");
                //}

                //
                // Add our base set of stocks to flesh out the UI
                //
                foreach (string s in _defaultStocks)
                {
                    ListViewItem li = StockDetails.Items.Add(s);
                    li.SubItems.Add("na");
                    li.SubItems.Add("na");
                    li.SubItems.Add("na");
                    li.SubItems.Add("na");
                    li.SubItems.Add("na");
                    li.SubItems.Add("na");
                    li.SubItems.Add("na");
                    li.SubItems.Add("na");
                }
            }
        }
    
        private void ClearQuotes_Click(object sender, EventArgs e)
        {
            //
            // Clear the listview items...
            //
            StockDetails.Items.Clear();
        }
    
        private void AddSymbol_Click(object sender, EventArgs e)
        {
            //
            // Make sure we don't already have this symbol
            //
            foreach (ListViewItem eli in StockDetails.Items)
            {
                if (eli.Text == StockSymbol.Text)
                {
                    MessageBox.Show("Symbol alreadys exists");
                    return;
                }
            }
            
            //
            // New symbol - add it to the list.
            //
            ListViewItem li = StockDetails.Items.Add(StockSymbol.Text);
            
            li.SubItems.Add("na");
            li.SubItems.Add("na");
            li.SubItems.Add("na");
            li.SubItems.Add("na");
            li.SubItems.Add("na");
            li.SubItems.Add("na");
            li.SubItems.Add("na");
            li.SubItems.Add("na");
        }
    
        private void UpdateEntry(StockQuote quote)
        {
            //
            // Match reponse to symbol in list...
            //
            foreach (ListViewItem li in StockDetails.Items)
            {
                if (li.Text == quote.Symbol)
                {
                    //
                    // Found the entry - update formatted results
                    //
                    li.SubItems[1].Text = StockQuote.FormatPrice(quote.Last);
                    li.SubItems[2].Text = StockQuote.FormatPrice(quote.Change);
                    li.SubItems[3].Text = quote.PercentChange;
                    li.SubItems[4].Text = StockQuote.FormatPrice(quote.High);
                    li.SubItems[5].Text = StockQuote.FormatPrice(quote.Low);
                    li.SubItems[6].Text = StockQuote.FormatVolume(quote.Volume);

                    li.SubItems[7].Text = quote.Date.ToString();
                    li.SubItems[8].Text = quote.Name;

                    //
                    // Update Color
                    //
                    try
                    {
                        double change = Convert.ToDouble(quote.Change);
                        if (change < 0.0)
                        {
                            li.ForeColor = Color.Red;
                        }
                        else if (change > 0.0)
                        {
                            li.ForeColor = Color.Green;
                        }
                        else
                        {
                            li.ForeColor = Color.Black;
                        }
                    }
                    catch {}                
                    break;
                }
            }
        }
    
        private void Ticker_Click(object sender, EventArgs e)
        {
            if (stockTicker.Enabled == true)
            {
                Ticker.Text         = "Start Ticker";
                Quote.Enabled       = true;
                ClearQuotes.Enabled = true;
                AddSymbol.Enabled   = true;

                stockTicker.Enabled = false;
            }
            else
            {
                Quote_Click(null, null);

                stockTicker.Enabled = true;

                Ticker.Text         = "Stop Ticker";
                Quote.Enabled       = false;
                ClearQuotes.Enabled = false;
                AddSymbol.Enabled   = false;
            }


        }
    
        private void stockTicker_Tick(object sender, EventArgs e)
        {
            Quote_Click(null, null);
        }
    #endregion Windows Application
    
        //
        // *** SQS Events ***
        //

        private void Quote_Click(object sender, EventArgs e)
        {
            //
            // Walk thru our list of symbol - make a request for each
            //
            foreach (ListViewItem li in StockDetails.Items)
            {
                requestor.MakeRequest(li.Text);
            }
        }

        private bool Requestor_ResponseEvent(SQS.Requestor requestor, SQS.SQSMessage response)
        {
            //
            // Received an incoming event from the queue...
            //
            StockQuote quote = new StockQuote(response._payload);    // convert msg to StockQuote
            UpdateEntry(quote);
            
            return true;
        }
    
        //
        // *** Form Startup / Shutdown ***
        //

        private void Form1_Load(object sender, EventArgs e)
        {
            //
            // Form's finished loading - start up our worker threads
            //
            requestor.Start();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {            
            //
            // Form's closed - shut everything down...
            //
            requestor.Stop();
        
            //
            // Save stocks
            //
            Properties.Settings.Default.Stocks = new System.Collections.Specialized.StringCollection();
            foreach (ListViewItem li in StockDetails.Items)
            {
                Properties.Settings.Default.Stocks.Add(li.Text);
            }

            Properties.Settings.Default.Save();
        }

    }
}